﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadVinaglue.Models.ViewPages;
using System.IO;
using System.Configuration;

namespace LeadVinaglue.Areas.Management.Controllers
{
    public class ArchiveListController : Controller
    {
        private static string fileName = "";
        public static string staFileName
        {
            get { return fileName; }
            set { fileName = value; }
        }

        // Create an instance of user manage
        LeadVinaglue.Repository.ArchiveManagementRepository repository = new LeadVinaglue.Repository.ArchiveManagementRepository();
        public static string ArchiveImagesURL = ConfigurationManager.AppSettings["ArchiveImagesURL"].ToString();

        //
        // GET: /Management/StateList/
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Index()
        {
            ArchiveManagementModel model = new ArchiveManagementModel();
            model = repository.GetModel(Helpers.Constant.HOME, "SuperAdministrator");

            return View(model);
        }

        //
        // GET: /Management/ArchiveList/Edit/id
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Add()
        {
            ArchiveManagementModel model = new ArchiveManagementModel();
            model = repository.GetModelAdd(Helpers.Constant.HOME, "SuperAdministrator");

            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Add(FormCollection form)
        {
            Archive model = new Archive();

            model.Title = form["Title"].ToString();
            model.ArchiveTypeID = int.Parse(form["ArchiveTypeId"].ToString());
            model.Active = (form["Active"] != null && form["Active"].ToString() == "on");
            model.ImageURL = form["ImageURL"].ToString();
            model.ShortDesc = form["ShortDesc"].ToString();
            model.LongDesc = form["LongDesc"].ToString();
            model.SortOrder = 0;

            repository.Create(model);

            return RedirectToAction("Index", "ArchiveList");
        }

        //
        // GET: /Management/ArchiveList/Edit/id
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id)
        {
            ArchiveManagementModel model = new ArchiveManagementModel();
            model = repository.GetModelEdit(Helpers.Constant.HOME, "SuperAdministrator", id);
            ViewBag.ArchiveId = id;

            return View(model);
        }

        //
        // GET: /Management/ArchiveList/Edit/id
        [HttpPost]
        [ValidateInput(false)]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Edit(int id, FormCollection form)
        {
            Archive model = repository.GetSingle(id);

            if (model != null)
            {
                model.Title = form["Title"].ToString();
                model.ArchiveTypeID = int.Parse(form["ArchiveTypeId"].ToString());
                model.ImageURL = form["ImageURL"].ToString();
                model.ShortDesc = form["ShortDesc"].ToString();
                model.LongDesc = form["LongDesc"].ToString();
                model.Active = (form["Active"] != null && form["Active"].ToString() == "on");

                repository.Save(model);
            }

            return RedirectToAction("Index", "ArchiveList");
        }

        // Delete Email Item
        [HttpGet]
        [Authorize(Roles = "SuperAdministrator")]
        public ActionResult Delete(int id)
        {
            repository.Delete(id);

            ArchiveManagementModel model = new ArchiveManagementModel();
            model = repository.GetModelDelete(Helpers.Constant.HOME, "SuperAdministrator");

            return Json(model, JsonRequestBehavior.AllowGet);
        }


        //[HttpPost]
        public ActionResult Upload(HttpPostedFileBase fileData)
        {
            // Verify that the user selected a file
            UserModel model = new UserModel();
            if (fileData != null && fileData.ContentLength > 0)
            {
                // extract only the fielname
                fileName = Path.GetFileName(fileData.FileName);

                // store the file inside ~/App_Data/uploads folder
                var path = Path.Combine(Server.MapPath(ArchiveImagesURL), fileName);
                fileData.SaveAs(path);
            }
            // redirect back to the index action to show the form once again
            return Json(model);
        }

        [HttpGet]
        public ActionResult UploadComplete()
        {
            return Json(string.Format("{0}/{1}", ArchiveImagesURL, fileName), JsonRequestBehavior.AllowGet);
        }
    }
}
